<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="省/市/区县:" prop="qurery_pcrs" label-width="100px">
        <area-cascader
          class="area-cascade-large"
          type="text"
          v-if="showQueryPacc"
          v-model="qurery_pcrs"
          :data="$pcaa"
          :level="2"
          separator="/"
        >
        </area-cascader>
      </el-form-item>
      <el-form-item label="检测地点" prop="place_name">
        <el-input
          v-model="queryParams.place_name"
          placeholder="请输入检测地点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!--
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
        -->
      <!--      <right-toolbar-->
      <!--    :showSearch.sync="showSearch"-->
      <!--    @queryTable="getList"-->
      <!--  ></right-toolbar>-->
    </el-row>
    <el-table
      v-loading="loading"
      :data="checkPlaceList"
      border
      :row-class-name="tableRowClassName"
      style="width: 100%;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" label="序号"  type="index" width="60"></el-table-column>
      <el-table-column label="编号" align="center" prop="uuid" width="300" />
      <el-table-column label="地址" align="center" prop="pcrs" min-width="60px" />
      <el-table-column
        label="详细地址"
        align="center"
        prop="place_name"
        min-width="60px"
      />
      <el-table-column label="坐标" align="center" prop="lon_lat" width="200" />
      <el-table-column
        label="创建者"
        align="center"
        prop="createBy"
        min-width="60px"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total , sizes , prev, pager, next , jumper"
      :page-size="queryParams.pageSize"
      :page-sizes="[5, 10, 20, 50]"
      :total="total"
      :current-page="queryParams.pageNum"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    <!-- 添加或修改公告对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      :before-close="back"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="18">
            <el-form-item label="省/市/区县:" prop="pacc" label-width="100px">
              <area-cascader
                v-if="showPacc"
                class="area-cascade-large"
                type="text"
                v-model="pcrs"
                :data="$pcaa"
                :level="2"
                separator="/"
              >
              </area-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item
              label="检测地点:"
              prop="place_name"
              label-width="100px"
            >
              <el-input
                v-model="form.place_name"
                placeholder="请输入检测地点"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCheckPlace,
  getCheckPlaceList,
  getCheckPlace,
  updateCheckPlace,
  delCheckPlace,
} from "@/api/check-place/check-place";
export default {
  name: "check-place",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      uuids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      checkPlaceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        province: "",
        city: "",
        region: "",
        street: "",
        place_name: "",
        createBy: "",
      },
      showPacc: false, // 弹出框中的省市区控件框，默认胃不显示
      showQueryPacc: true, // 查询框中的省市区控件框，默认显示
      // 省-市-区-街道
      pcrs: [],
      qurery_pcrs: [],
      // 表单参数
      form: {
        province: "",
        city: "",
        region: "",
        street: "",
        place_name: "",
      },
      // 表单校验
      rules: {
        place_name: [
          { required: true, message: "监测点不能为空", trigger: "blur" },
        ],
        qurery_pcrs: [
          { required: true, message: "省/市/区/街道信息不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return "";
      } else {
        return "";
      }
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      // 将省市区控件中的数组，转成对应的省、市、区字段
      this.queryParams = this.switchPcrs(this.qurery_pcrs, this.queryParams);
      let params = this.$qs.stringify(this.queryParams);
      getCheckPlaceList(params).then((response) => {
        this.checkPlaceList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 分页页码改变时
    handleSizeChange(size) {
      this.queryParams.pageSize = size;
      this.getList();
    },
    handleCurrentChange(current) {
      this.queryParams.pageNum = current;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.showPacc = false;
      this.open = false;
      this.reset();
    },
    // 弹出框关闭按钮触发事件
    back() {
      this.open = false;
      this.showPacc = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        province: "",
        city: "",
        region: "",
        street: "",
        place_name: "",
      };
    },
    // 处理pcrs,将其转存为form.province,city,region,street
    switchPcrs(pcrs, form) {
      if (pcrs.length !== 0) {
        form.province = pcrs[0];
        form.city = pcrs[1];
        form.region = pcrs[2];
        form.street = pcrs[3];
      }
      return form;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.showQueryPacc = false;
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        province: "",
        city: "",
        region: "",
        street: "",
        place_name: "",
        createBy: "",
      };
      this.qurery_pcrs = [];
      this.showQueryPacc = true;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.uuids = selection.map((item) => item.uuid);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.showPacc = true;
      this.title = "新增检测点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uuid = row.uuid;
      getCheckPlace(uuid).then((response) => {
        this.form = response.data;
        this.pcrs = response.data.pcrs; //填充控件的值
        this.open = true;
        this.showPacc = true;
        this.title = "修改检测点";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.uuid != undefined) {
            // 更新form字典中，provinc，city，region，street信息
            this.form = this.switchPcrs(this.pcrs, this.form);
            let params = this.$qs.stringify(this.form);
            updateCheckPlace(params).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.showPacc = false;
              this.getList();
            });
          } else {
            this.form = this.switchPcrs(this.pcrs, this.form);
            let params = this.$qs.stringify(this.form);
            addCheckPlace(params).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.showPacc = false;
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uuids = row.uuid || this.uuids;
      this.$modal
        .confirm('是否确认删除编号为"' + uuids + '"的数据项？')
        .then(function () {
          return delCheckPlace(uuids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss">
.area-select.large {
  width: 100%;
  line-height: 20px;
}
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
